home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevm16.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  5.4 KB  |  179 lines

  1. /* Copyright (C) 1994, 1996, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevm16.c,v 1.2 2000/09/19 19:00:13 lpd Exp $ */
  20. /* 16-bit-per-pixel "memory" (stored bitmap) device */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gxdevice.h"
  24. #include "gxdevmem.h"        /* semi-public definitions */
  25. #include "gdevmem.h"        /* private definitions */
  26.  
  27. #undef chunk
  28. #define chunk byte
  29.  
  30. /* The 16 bits are divided 5 for red, 6 for green, and 5 for blue. */
  31. /* Note that the bits must always be kept in big-endian order. */
  32.  
  33. /* Procedures */
  34. declare_mem_map_procs(mem_true16_map_rgb_color, mem_true16_map_color_rgb);
  35. declare_mem_procs(mem_true16_copy_mono, mem_true16_copy_color, mem_true16_fill_rectangle);
  36.  
  37. /* The device descriptor. */
  38. const gx_device_memory mem_true16_device =
  39.     mem_device("image16", 16, 0,
  40.            mem_true16_map_rgb_color, mem_true16_map_color_rgb,
  41.            mem_true16_copy_mono, mem_true16_copy_color,
  42.            mem_true16_fill_rectangle, mem_default_strip_copy_rop);
  43.  
  44. /* Map a r-g-b color to a color index. */
  45. private gx_color_index
  46. mem_true16_map_rgb_color(gx_device * dev, gx_color_value r, gx_color_value g,
  47.              gx_color_value b)
  48. {
  49.     return ((r >> (gx_color_value_bits - 5)) << 11) +
  50.     ((g >> (gx_color_value_bits - 6)) << 5) +
  51.     (b >> (gx_color_value_bits - 5));
  52. }
  53.  
  54. /* Map a color index to a r-g-b color. */
  55. private int
  56. mem_true16_map_color_rgb(gx_device * dev, gx_color_index color,
  57.              gx_color_value prgb[3])
  58. {
  59.     ushort value = color >> 11;
  60.  
  61.     prgb[0] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4))
  62.               >> (16 - gx_color_value_bits);
  63.     value = (color >> 5) & 0x3f;
  64.     prgb[1] = ((value << 10) + (value << 4) + (value >> 2))
  65.               >> (16 - gx_color_value_bits);
  66.     value = color & 0x1f;
  67.     prgb[2] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4))
  68.               >> (16 - gx_color_value_bits);
  69.     return 0;
  70. }
  71.  
  72. /* Convert x coordinate to byte offset in scan line. */
  73. #undef x_to_byte
  74. #define x_to_byte(x) ((x) << 1)
  75.  
  76. /* Fill a rectangle with a color. */
  77. private int
  78. mem_true16_fill_rectangle(gx_device * dev,
  79.               int x, int y, int w, int h, gx_color_index color)
  80. {
  81.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  82. #if arch_is_big_endian
  83.     const ushort color16 = (ushort)color;
  84. #else
  85.     const ushort color16 = (ushort)((color << 8) | (color >> 8));
  86. #endif
  87.     declare_scan_ptr(dest);
  88.  
  89.     fit_fill(dev, x, y, w, h);
  90.     setup_rect(dest);
  91.     if (w == 1) {
  92.     while (h-- > 0) {
  93.         *(ushort *)dest = color16;
  94.         inc_ptr(dest, draster);
  95.     }
  96.     } else if ((color16 >> 8) == (color16 & 0xff)) {
  97.     bytes_fill_rectangle(scan_line_base(mdev, y) + (x << 1), draster,
  98.                  (byte)color, w << 1, h);
  99.     } else {
  100.     while (h-- > 0) {
  101.         ushort *pptr = (ushort *) dest;
  102.         int cnt = w;
  103.  
  104.         for (; cnt >= 4; pptr += 4, cnt -= 4)
  105.         pptr[3] = pptr[2] = pptr[1] = pptr[0] = color16;
  106.         switch (cnt) {
  107.         case 3: pptr[2] = color16;
  108.         case 2: pptr[1] = color16;
  109.         case 1: pptr[0] = color16;
  110.         case 0: DO_NOTHING;
  111.         }
  112.         inc_ptr(dest, draster);
  113.     }
  114.     }
  115.     return 0;
  116. }
  117.  
  118. /* Copy a monochrome bitmap. */
  119. private int
  120. mem_true16_copy_mono(gx_device * dev,
  121.              const byte * base, int sourcex, int sraster,
  122.              gx_bitmap_id id, int x, int y, int w, int h,
  123.              gx_color_index zero, gx_color_index one)
  124. {
  125.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  126. #if arch_is_big_endian
  127.     const ushort zero16 = (ushort)zero;
  128.     const ushort one16 = (ushort)one;
  129. #else
  130.     ushort zero16 = ((uint) (byte) zero << 8) + ((ushort) zero >> 8);
  131.     ushort one16 = ((uint) (byte) one << 8) + ((ushort) one >> 8);
  132. #endif
  133.     const byte *line;
  134.     int first_bit;
  135.  
  136.     declare_scan_ptr(dest);
  137.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  138.     setup_rect(dest);
  139.     line = base + (sourcex >> 3);
  140.     first_bit = 0x80 >> (sourcex & 7);
  141.     while (h-- > 0) {
  142.     register ushort *pptr = (ushort *) dest;
  143.     const byte *sptr = line;
  144.     register int sbyte = *sptr++;
  145.     register int bit = first_bit;
  146.     int count = w;
  147.  
  148.     do {
  149.         if (sbyte & bit) {
  150.         if (one != gx_no_color_index)
  151.             *pptr = one16;
  152.         } else {
  153.         if (zero != gx_no_color_index)
  154.             *pptr = zero16;
  155.         }
  156.         if ((bit >>= 1) == 0)
  157.         bit = 0x80, sbyte = *sptr++;
  158.         pptr++;
  159.     }
  160.     while (--count > 0);
  161.     line += sraster;
  162.     inc_ptr(dest, draster);
  163.     }
  164.     return 0;
  165. }
  166.  
  167. /* Copy a color bitmap. */
  168. private int
  169. mem_true16_copy_color(gx_device * dev,
  170.            const byte * base, int sourcex, int sraster, gx_bitmap_id id,
  171.               int x, int y, int w, int h)
  172. {
  173.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  174.  
  175.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  176.     mem_copy_byte_rect(mdev, base, sourcex, sraster, x, y, w, h);
  177.     return 0;
  178. }
  179.